package com.lyy.mybatis.sensitive.mask;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

import java.io.IOException;

public final class SensitiveSerializer extends StdScalarSerializer<Object> {
    private final IMaskStrategy strategy;


    public SensitiveSerializer(IMaskStrategy strategy) {
        super(String.class, false);
        this.strategy = strategy;
    }


    @Override
    public boolean isEmpty(SerializerProvider prov, Object value) {
        return value == null || StrUtil.isEmpty((String) value);
    }

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        String content = strategy.handle((String) value);
        gen.writeString(content);
    }


}